﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DALFactory;
using KPIS.DBM;

namespace KPIS.GERP.GHRMS.BLL
{
    public class IncomeTypeBLL : IIncomeTypeBLL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public DataSet ListIncomeType(Nullable<int> Sequence)
        {
            dbManager.CreateParameters(2);
            dbManager.AddParameters(0, "@RecordStatus", SystemInfo._DeleteRecordStatus);
            dbManager.AddParameters(1, "@Sequence", Sequence);

            DataSet ds = new DataSet();
            dbManager.Open();
            ds = dbManager.ExecuteDataSet(CommandType.Text
               , "select * from HRM_PR_EXTRA_TYPES as ET"
               + " left join hrm_ps_command_types CT on ET.cmd_type_seq = CT.cmd_type_seq"
               + " left join mas_record_statuses RS on ET.record_status = RS.record_status"
               + " where ET.record_status not like @RecordStatus"
               + (Sequence.HasValue ? " and ET.ext_seq = @Sequence" : "")
               + " and ET.ext_flag = 'I'");
            dbManager.Dispose();
            return ds;
        }

        public DataSet ListIncomeType(int emp_type_seq, string record_status)
        {
            dbManager.CreateParameters(2);
            dbManager.AddParameters(0, "@emp_type_seq", emp_type_seq);
            dbManager.AddParameters(1, "@RecordStatus", record_status);

            DataSet ds = new DataSet();
            dbManager.Open();
            ds = dbManager.ExecuteDataSet(CommandType.Text
               , "select * from HRM_PR_EXTRA_TYPES as ET"
               + " left join hrm_ps_command_types CT on ET.cmd_type_seq = CT.cmd_type_seq"
               + " left join mas_record_statuses RS on ET.record_status = RS.record_status"
               + " where ET.emp_type_seq = @emp_type_seq "
               + " and ET.record_status = @RecordStatus"
               + " and ET.ext_flag = 'I'");
            dbManager.Dispose();
            return ds;
        }

        public void Insert(int emp_type_seq, string ext_code, string ext_name, string ext_name_short
            , Nullable<double> ext_amount, Nullable<int> cmd_type_seq, string ext_flag, string ext_tax
            , string ext_monthly, string remarks, string record_status, string CreatedBy)
        {
            try
            {
                dbManager.CreateParameters(13);
                dbManager.AddParameters(0, "@emp_type_seq", emp_type_seq);
                dbManager.AddParameters(1, "@ext_code", ext_code);
                dbManager.AddParameters(2, "@ext_name", ext_name);

                dbManager.AddParameters(3, "@ext_name_short", ext_name_short);
                dbManager.AddParameters(4, "@cmd_type_seq", cmd_type_seq);
                dbManager.AddParameters(5, "@ext_flag", ext_flag);
                dbManager.AddParameters(6, "@ext_tax", ext_tax);
                dbManager.AddParameters(7, "@ext_amount", ext_amount);
                dbManager.AddParameters(8, "@ext_monthly", ext_monthly);

                dbManager.AddParameters(9, "@remarks", remarks);
                dbManager.AddParameters(10, "@RecordStatus", record_status);
                dbManager.AddParameters(11, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(12, "@CreatedWhen", DateTime.Today);

                dbManager.Open();
                dbManager.ExecuteNonQuery(CommandType.Text
                    , "INSERT INTO HRM_PR_EXTRA_TYPES"
                    + " (emp_type_seq, ext_code, ext_name, ext_name_short, cmd_type_seq, ext_flag, ext_tax, ext_amount, ext_monthly"
                    + ", remarks, record_status,created_by,created_when)"
                    + " VALUES (@emp_type_seq, @ext_code, @ext_name, @ext_name_short, @cmd_type_seq, @ext_flag, @ext_tax, @ext_amount"
                        + ", @ext_monthly, @remarks, @RecordStatus,@CreatedBy,@CreatedWhen)");
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }

        public void Update(int Sequence, int emp_type_seq, string ext_code, string ext_name, string ext_name_short
            , Nullable<double> ext_amount, Nullable<int> cmd_type_seq, string ext_flag, string ext_tax
            , string ext_monthly, string remarks, string record_status, string UpdateBy)
        {
            try
            {
                dbManager.CreateParameters(14);
                dbManager.AddParameters(0, "@sequence", Sequence);
                dbManager.AddParameters(1, "@emp_type_seq", emp_type_seq);
                dbManager.AddParameters(2, "@ext_code", ext_code);
                dbManager.AddParameters(3, "@ext_name", ext_name);
                dbManager.AddParameters(4, "@ext_name_short", ext_name_short);
                dbManager.AddParameters(5, "@cmd_type_seq", cmd_type_seq);
                dbManager.AddParameters(6, "@ext_flag", ext_flag);
                dbManager.AddParameters(7, "@ext_tax", ext_tax);
                dbManager.AddParameters(8, "@ext_amount", ext_amount);
                dbManager.AddParameters(9, "@ext_monthly", ext_monthly);
                dbManager.AddParameters(10, "@remarks", remarks);
                dbManager.AddParameters(11, "@RecordStatus", record_status);
                dbManager.AddParameters(12, "@updated_by", UpdateBy);
                dbManager.AddParameters(13, "@updated_when", DateTime.Today);
                dbManager.Open();

                dbManager.ExecuteNonQuery(CommandType.Text
                    , "UPDATE HRM_PR_EXTRA_TYPES"
                    + " SET emp_type_seq=@emp_type_seq"
                        + ", ext_code=@ext_code"
                        + ", ext_name=@ext_name"
                        + ", ext_name_short=@ext_name_short"
                        + ", cmd_type_seq=@cmd_type_seq"
                        + ", ext_flag=@ext_flag"
                        + ", ext_tax=@ext_tax"
                        + ", ext_amount=@ext_amount"
                        + ", ext_monthly=@ext_monthly"
                        + ", remarks=@remarks"
                        + ", record_status=@RecordStatus"
                        + ", updated_by=@updated_by"
                        + ", updated_when=@updated_when"
                    + " WHERE ext_seq=@sequence");
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }
        public void Delete(int Sequence, string UpdateBy)
        {
            try
            {
                dbManager.CreateParameters(4);
                dbManager.AddParameters(0, "@sequence", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdateBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);
                dbManager.Open();
                dbManager.ExecuteNonQuery(CommandType.Text, "UPDATE HRM_PR_EXTRA_TYPES SET RECORD_STATUS=@record_status,updated_by=@updated_by, updated_when=@updated_when WHERE ext_seq=@sequence");
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }

    }
}
